package link.jfire.socket.socketserver.bus;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import javax.annotation.Resource;
import link.jfire.baseutil.simplelog.ConsoleLogFactory;
import link.jfire.baseutil.simplelog.Logger;
import link.jfire.socket.socketserver.handler.EntryHandler;
import link.jfire.socket.socketserver.handler.MessageAction;
import link.jfire.socket.socketserver.interceptor.EntryInterceptor;
import link.jfire.socket.socketserver.storage.ringarray.ComplexMultRingArray;
import link.jfire.socket.socketserver.storage.ringarray.RingArray;
import link.jfire.socket.socketserver.storage.ringarray.SimpleMultRingArray;
import link.jfire.socket.socketserver.storage.waitstrategy.WaitStrategy;
import link.jfire.socket.socketserver.transfer.handler.socket.ChannelReadHandler;
import link.jfire.socket.socketserver.transfer.server.ServerConfig;
import link.jfire.socket.socketserver.util.HeadFactory;

@Resource
public class CenterSwitch
{
    @Resource
    private List<EntryHandler>     handlerList     = new ArrayList<>();
    @Resource
    private List<EntryInterceptor> interceptorList = new ArrayList<>();
    @Resource
    private ChannelReadHandler     readHandler;
    private RingArray              ringArray;
    private Logger                 logger          = ConsoleLogFactory.getLogger();
    private int                    size;
    @Resource
    private WaitStrategy           waitStrategy;
    @Resource
    private ServerConfig           serverConfig;
    private MessageAction[]        actions;
    @Resource
    private HeadFactory            headFactory;
    private ExecutorService        threadPool;
    
    public void start()
    {
        actions = new MessageAction[serverConfig.getHandlerThreadSize()];
        switch (serverConfig.getRingArrayType())
        {
            case ServerConfig.SimpleMult:
                ringArray = new SimpleMultRingArray(size, waitStrategy, actions);
                
                break;
            case ServerConfig.ComplexMult:
                ringArray = new ComplexMultRingArray(size, waitStrategy, actions);
        }
        logger.debug("加载的消息处理器有：");
        for (EntryHandler handler : handlerList)
        {
            logger.debug("{}", handler.getClass().getName());
        }
        logger.debug("加载的消息拦截器有：");
        for (EntryInterceptor interceptor : interceptorList)
        {
            logger.debug("{}", interceptor.getClass().getName());
        }
        readHandler.setRingArray(ringArray);
        for (int i = 0; i < actions.length; i++)
        {
            actions[i] = new MessageAction(interceptorList.toArray(new EntryInterceptor[0]), handlerList.toArray(new EntryHandler[0]), ringArray, headFactory);
        }
        threadPool = Executors.newFixedThreadPool(actions.length, new ThreadFactory() {
            int i = 0;
            
            @Override
            public Thread newThread(Runnable r)
            {
                return new Thread(r, "任务线程-" + (i++));
            }
        });
        for (int i = 0; i < actions.length; i++)
        {
            threadPool.execute(actions[i]);
        }
        
    }
    
    public void stop() throws InterruptedException
    {
        ringArray.stop();
        threadPool.shutdown();
    }
    
    public List<EntryHandler> getHandlerList()
    {
        return handlerList;
    }
    
    public List<EntryInterceptor> getInterceptorList()
    {
        return interceptorList;
    }
    
}
